import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:applemusicauth_plugin_t/applemusicauth_plugin_t.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _platformVersion = 'Unknown';
  final _applemusicauthPlugin_tPlugin = ApplemusicauthPlugin_t();

  @override
  void initState() {
    super.initState();
    initPlatformState();
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
    String platformVersion;
    // Platform messages may fail, so we use a try/catch PlatformException.
    // We also handle the message potentially returning null.
    try {
      platformVersion =
          await _applemusicauthPlugin_tPlugin.getPlatformVersion() ??
              'Unknown platform version';
    } on PlatformException {
      platformVersion = 'Failed to get platform version.';
    }

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;

    setState(() {
      _platformVersion = platformVersion;
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Center(
          child: Text('Running on: $_platformVersion\n'),
        ),
        floatingActionButton: TextButton(
          onPressed: () async {
            Map<String, dynamic>? map = await _applemusicauthPlugin_tPlugin.getAppleMusicAuth(
                "eyJhbGciOiJFUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6IlM3V1Q5UEs3VTUifQ.eyJpYXQiOjE2OTEwNTM4ODgsImV4cCI6MTY5NjIzNzg4OCwiaXNzIjoiUTQ4TDYzWjdMOCJ9.vRChpgBq4eB5TBzuQC2EN54-o742qbZQ6G0IZaugn056k7TKnAN5m4P3yYPxf36-T90XP_IN8lDzdHnPpYoFig");

            print("flutter收到回调 map = $map");
          },
          child: Text("点我授权"),
        ),
      ),
    );
  }
}
